package com.heima.admin.controller.v1;

import com.heima.admin.service.AdUserService;
import com.heima.common.exception.LeadNewsException;
import com.heima.model.admin.dto.AdUserDto;
import com.heima.model.admin.pojo.AdUser;
import com.heima.model.common.dto.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author Ronaldo
 * @date 2021/7/30
 */
@RestController
@RequestMapping("/login")
@Slf4j
@Api(value = "用户信息",tags = "user",description = "用户信息管理api")
public class AdUserController {

    @Autowired
    private AdUserService adUserService;

    @ApiOperation("登录校验")
    @PostMapping("/in")
    public ResponseResult login(@RequestBody AdUserDto adUserDto){

        log.info("login by {} " + adUserDto);

        //参数判断
        if (Objects.isNull(adUserDto)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (StringUtils.isBlank(adUserDto.getName())){
            throw new LeadNewsException(AppHttpCodeEnum.NOT_FOUND_USERNAME);
        }
        if (StringUtils.isBlank(adUserDto.getPassword())){
            throw new LeadNewsException(AppHttpCodeEnum.NOT_FOUND_PASSWORD);
        }

        return adUserService.login(adUserDto);
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public ResponseResult register(@RequestBody AdUser adUser){
        log.info("register user by {}" + adUser);
        //参数判断
        if (Objects.isNull(adUser)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return adUserService.register(adUser);

    }
}
